#include "ISNW_Button.h"
#include "../../ISNW_Collision/ISNW_Collsion.h"
using namespace ISNW;

ISNW_Button::ISNW_Button()
:m_currState(ISNW_NULL)
{
	Init();
}

ISNW_VOID ISNW_Button::Init()
{
	m_currState = &m_dead;
	m_normal.Init(this,&m_render);
	m_normal2focus.Init(this,&m_render);
	m_focus.Init(this,&m_render);
	m_focus2lclick.Init(this,&m_render);
	m_lclick.Init(this,&m_render);
	m_lclick2focus.Init(this,&m_render);
	m_focus2normal.Init(this,&m_render);
	m_normal2dead.Init(this,&m_render);
	m_dead.Init(this,&m_render);

	m_render.Init(this);

	m_text.SetParent(this);
}

ISNW_VOID ISNW_Button::SetFormat(ISNW_BUTTON_FORMAT f)
{
	m_format = f;
}

ISNW_VOID ISNW_Button::SetText(ISNW_StringBaby sb)
{
	m_text.SetText(sb);
}

ISNW_VOID ISNW_Button::Create()
{
	switch(m_format){
		case ISNW_BUTTON_CREATE_CUBE:
			iCreateCubeButton();
			m_currState = &m_normal;
			m_currState->Reset();
			break;
		default:
			break;
	}
	m_text.Create();
	m_text.SetPos(0,0,-40);
	D3DCOLORVALUE cv;
	cv.a = 1;cv.b = cv.g = cv.r = 0;
	cv.r = 1;
	m_text.SetColor(cv);
}

ISNW_VOID ISNW_Button::iCreateCubeButton()
{
	m_render.SetFormat(ISNW_ButtonRender::ISNW_BUTTONRENDER_CREATE_CUBE);
	m_render.Create();
}

ISNW_VOID ISNW_Button::Render()
{
	m_currState->Render();
	m_text.Render();
}

ISNW_VOID ISNW_Button::SetTextureFileSrc(ISNW_StringBaby sb)
{
	m_render.SetTexture(sb);
}

ISNW_VOID ISNW_Button::SetState(ISNW_BUTTON_STATE state)
{
	switch(state){
		case ISNW_BUTTON_STATE_DEAD:
			m_currState = &m_dead;
			break;
		case ISNW_BUTTON_STATE_NORMAL:
			m_currState = &m_normal;
			break;
		case ISNW_BUTTON_STATE_NORMALTODEAD:
			m_currState = &m_normal2dead;
			break;
		case ISNW_BUTTON_STATE_NORMALTOFOCUS:
			m_currState = &m_normal2focus;
			break;
		case ISNW_BUTTON_STATE_FOCUS:
			m_currState = &m_focus;
			break;
		case ISNW_BUTTON_STATE_FOCUSTOLCLICK:
			m_currState = &m_focus2lclick;
			break;
		case ISNW_BUTTON_STATE_LCLICK:
			m_currState = &m_lclick;
			break;
		case ISNW_BUTTON_STATE_LCLICKTOFOCUS:
			m_currState = &m_lclick2focus;
			break;
		case ISNW_BUTTON_STATE_FOCUSTONORMAL:
			m_currState = &m_focus2normal;
			break;
		default:
			break;
	}
	m_currState->Reset();
}

ISNW_CtrlInterface::ISNW_CTRL_STATE ISNW_Button::OnMouseEvent(ISNW_MouseInfo mi)
{
	if(!IsIn(mi.pos)){
		return ISNW_CtrlInterface::ISNW_CTRL_NULL;
	}
	if(mi.info & INPUT_LBUTTON){
		if(mi.info & INPUT_DOWN){
			m_currState->OnLButtonDown();
			return ISNW_CtrlInterface::ISNW_CTRL_LOCKED;
		}else{
			m_currState->OnLButtonUp();
		}
	}
	if(mi.info & INPUT_RBUTTON){
		if(mi.info & INPUT_DOWN){
			m_currState->OnRButtonDown();
		}else{
			m_currState->OnRButtonUp();
		}
	}
	if(mi.info & INPUT_MBUTTON){
		if(mi.info & INPUT_DOWN){
			m_currState->OnMButtonDown();
		}else{
			m_currState->OnMButtonUp();
		}
	}
	return ISNW_CtrlInterface::ISNW_CTRL_NULL;
}

ISNW_BOOL ISNW_Button::IsIn(POINT p)
{
	D3DXMATRIX translate;
	D3DXVECTOR3 pos = Cvt2ScreenPos();
	D3DXVECTOR3 _pos = D3DXVECTOR3(0,0,0);
	_pos.z = pos.z;
	_pos.x = (pos.x - g_pApp->Width()/2)*2.0f*_pos.z/g_pApp->Height();
	_pos.y = (g_pApp->Height()/2 - pos.y)*2.0f*_pos.z/g_pApp->Height();
	D3DXMatrixTranslation(&translate,_pos.x,_pos.y,_pos.z);
	return m_render.IsIn(p,translate);
}

ISNW_CtrlInterface::ISNW_CTRL_STATE ISNW_Button::ProcessCursor(POINT pos)
{
	if(IsIn(pos)){
		m_currState->OnGetFocus();
		return ISNW_CtrlInterface::ISNW_CTRL_GETFOCUS;
	}else{
		m_currState->OnLoseFocus();
		return ISNW_CtrlInterface::ISNW_CTRL_LOSTFOCUS;
	}
}
